package com.ruoyi.orders.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.orders.pojo.ZxGoods;
import com.ruoyi.orders.pojo.ZxOrders;
import com.ruoyi.orders.pojo.dto.ZxOrdersDto;
import com.ruoyi.orders.service.ZxOrdersService;
import com.ruoyi.orders.mapper.ZxOrdersMapper;
import com.ruoyi.orders.pojo.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.security.Security;
import java.util.List;

/**
* @author 陈嘉俊
* @description 针对表【zx_orders(订单)】的数据库操作Service实现
* @createDate 2023-12-18 16:11:50
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class ZxOrdersServiceImpl extends ServiceImpl<ZxOrdersMapper, ZxOrders>
    implements ZxOrdersService{

    @Autowired
    protected Validator validator;
    @Autowired
    private  ZxOrdersMapper mapper;

    @Override
    public Boolean orderAble(ZxGoods goods) {
        if (mapper.checkAble(goods)>=0){
            return true;
        }
        return false;

    }

    @Override
    public String importData(List<ZxOrders> ordersList, String operName) {
        if (StringUtils.isNull(ordersList) || ordersList.size() == 0) {
            throw new ServiceException("导入数据不能为空!");
        }
        int successNum = 0;//成功条数
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (ZxOrders dto : ordersList) {
            try {
                BeanValidators.validateWithException(validator,dto);
                this.saveOrUpdate(dto);
                successNum++;
                successMsg.append("<br/>" + successNum + "、导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    @Override
    public Boolean doOrder(ZxOrdersDto ordersDto) throws Exception {

//            构建订单

            String s = UUID.randomUUID().toString();
            ordersDto.setOrderCode(s);
            ordersDto.setOrderTime(DateUtils.getTime());
//            判断能否下订单
            for(ZxGoods goods:ordersDto.getZxGoodsList()){
                Boolean aBoolean = orderAble(goods);
                if (!aBoolean){
                    throw new Exception("库存不足");
                }
            }
            try {
                //            添加订单
                mapper.insertAndGetAutoId(ordersDto);
//            减库存
                mapper.updataGoodsNumById(ordersDto.getZxGoodsList());
//                维护中间
                mapper.insertOrderGoodsCord(ordersDto);
                mapper.insertUserOrders(SecurityUtils.getUserId(),ordersDto.getId());
            }catch (Exception e){
                throw e;
            }


            return true;
    }


    @Override
    public List<ZxOrdersDto> findListByGoodsName(String names) {

        String[] params = null;
        if (!StringUtils.isEmpty(names)){
            params = names.split(" ");
        }
        return mapper.findListByGoodsName(params);
    }


    @Override
    public void deleteByIds(List<Integer> ids) {
         mapper.orderRemoveByIds(ids);
         mapper.order_user(ids);
         mapper.order_goods(ids);
    }

    @Override
    public List<ZxOrders> getList() {
        return  mapper.getlist();
    }
}




